package com.cmcc.passenger.ui.fragment

import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import androidx.fragment.app.DialogFragment
import com.cmcc.passenger.R
import com.cmcc.passenger.databinding.DialogCancelReasonBinding
import com.cmcc.shared.utils.clickWithTrigger

class CancelReasonDialogFragment : DialogFragment() {

    private var onCommitClickListener: OnCommitClickListener? = null

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        if (context == null || !isAdded) return super.onCreateDialog(savedInstanceState)
        val binding = DialogCancelReasonBinding.inflate(LayoutInflater.from(context), null, false)
//        val inflater = LayoutInflater.from(context)
        val dialog = Dialog(context!!, R.style.TransparentStyleBottomDialog)
        dialog.setContentView(binding.root)

        val m = activity!!.windowManager
        val d = m.defaultDisplay
        val p = dialog.window!!.attributes
        p.width = (d.width * 0.9).toInt()
        dialog.window!!.attributes = p
        binding.navigationButton.clickWithTrigger { dismiss() }
        binding.positiveButton.clickWithTrigger {
            val reason =
                when (binding.groupCancelReason.checkedRadioButtonId) {
                    R.id.reason_one -> getString(R.string.cancel_call_reason_one)
                    R.id.reason_two -> getString(R.string.cancel_call_reason_two)
                    R.id.reason_three -> getString(R.string.cancel_call_reason_three)
                    R.id.reason_four -> getString(R.string.cancel_call_reason_four)
                    R.id.reason_five -> getString(R.string.cancel_call_reason_five)
                    else -> getString(R.string.cancel_call_reason_one)
                }
            onCommitClickListener?.onCommitClick(reason)
            dismiss()
        }
//        binding.content.text = "180s \n由於你無辜取消訂單，請3分\n鐘後再使用叫車服務"
        return dialog
    }

    fun setOnCommitClickListener(listener: OnCommitClickListener) {
        this.onCommitClickListener = listener
    }

    companion object {

        fun newFragment(): CancelReasonDialogFragment {
            return CancelReasonDialogFragment()
        }
    }

    interface OnCommitClickListener {
        fun onCommitClick(remark: String)
    }

}